//
//  StatisticsViewModel.swift
//  SLQuickReader
//
//  Created by ALin on 2017/8/3.
//  Copyright © 2017年 ALin. All rights reserved.
//

import UIKit
import RxSwift

let StatisticsBtnHeight: CGFloat = 50.0

class StatisticsViewModel: BaseViewModel, BaseView {
    var statistics: [String: [Statistics]] = [:]
    
    func bind(to view: UIView...) {
        
        guard view.first is StatisticsCell else{
            return
        }
        
        let cell = view.first as! StatisticsCell
        let key = statistics.keys.first ?? ""
        cell.title.text = key
        cell.title.textColor = statisticsTitleColor(key)
        
        generateBtns(statistics.values.first, parentView: cell.tagsView, selected: cell.title.textColor)
    }
    
    func generateBtns(_ tags: [Statistics]?, parentView: UIView, selected color: UIColor) {
        guard let tags = tags else {
            return
        }
        
        _ = parentView.subviews
            .filter{ $0 is RImageButton}
            .map{ $0.removeFromSuperview() }
        
        let width = screenWidth / 3.0
        let height: CGFloat = StatisticsBtnHeight
        var x: CGFloat = 0
        var y: CGFloat = 0
        let col: Int = 3
        let count = tags.count
        let rows = (count % 3 == 0) ? (count / 3) : (count / 3 + 1)
        parentView.snp.updateConstraints{
            $0.height.equalTo(CGFloat(rows) * height)
        }
        
        for (i, stat) in tags.enumerated(){
            let btn = RImageButton().then{
                $0.titleLabel?.font = UIFont.systemFont(ofSize: 14)
                $0.setTitle(stat.name, for: .normal)
                $0.setTitleColor(UIColor.color(r: 52, g: 60, b: 71), for: .normal)
                $0.setTitleColor(color, for: .highlighted)
                x = CGFloat(i % col) * width
                y = CGFloat(i / col) * height
                $0.frame = CGRect(x: x, y: y, width: width, height: height)
                
                if stat.isHot{
                    $0.setImage(UIImage(named: "category_hot_13x15"), for: .normal)
                }
                
                if i < rows{
                    // 添加顶部的线
                    let topLine = UIView().then{
                        $0.backgroundColor = UIColor.lightGray
                        $0.frame = CGRect(x: 0, y: CGFloat(i) * height, width: screenWidth, height: margin1px)
                    }
                    parentView.addSubview(topLine)
                }
                
                // 添加右侧的线
                let rLine = UIView().then{
                    $0.backgroundColor = UIColor.lightGray
                    $0.frame = CGRect(x: width, y: 0, width: margin1px, height: height * 3.0 / 5.0)
                    $0.centerY = height * 0.5
                }
                $0.addSubview(rLine)

                // 点击事件
                _ = $0.rx.tap.flatMapLatest{
                    Observable.just(stat.name!)
                }.subscribe(onNext: { category in
                    NotificationCenter.default.post(name: NSNotification.Name(rawValue: BookStoreTapStatisticsNotify), object: nil, userInfo: [UserInfo: category])
                })
            }
            parentView.addSubview(btn)
        }
    }

}

extension StatisticsViewModel{
    func statisticsTitleColor(_ key: String) -> UIColor{
        switch key {
        case maleTitle:
            return UIColor.color(r: 58, g: 155, b: 255)
        case femaleTitle:
            return UIColor.color(r: 255, g: 126, b: 188)
        case pictureTitle:
            return UIColor.color(r: 123, g: 218, b: 109)
        case pressTitle:
            return UIColor.color(r: 193, g: 139, b: 248)
        default:
            return UIColor.green
        }
    }
}
